import { access } from "fs";

const TokenKey = 'accesstoken';
const TokenPrefix = '';
const Tokensuffix = '';

const isLogin = () => {
  return !!localStorage.getItem(TokenKey)
}
const getToken = () => {
  // const accesstoken = localStorage.getItem(TokenKey);
  // console.log(accesstoken)
  // return accesstoken?accesstoken:undefined;

  return localStorage.getItem(TokenKey);
}
const setToken = (token: string) => {
  localStorage.setItem(TokenKey, token)
}
const clearToken = () => {
  localStorage.removeItem(TokenKey)
}

const courseTypesCache = 'course-types';
const setCourseTypes = (courseTypes) => {
  if(courseTypes === undefined || courseTypes === null){
    return false;
  }
  localStorage.setItem(courseTypesCache, JSON.stringify(courseTypes));
}

const getCourseTypes = () => {
  let courseTypes = localStorage.getItem(courseTypesCache);
  if(courseTypes === undefined || courseTypes === null){
    return undefined;
  }
  if(courseTypes === ''){ // 空字符串
    return undefined;
  }
  return JSON.parse(courseTypes || '{}'); //courseTypes || undefined;
}

const clearCourseTypes = () => {
  localStorage.removeItem(courseTypesCache);
}

export { TokenKey, TokenPrefix, Tokensuffix, isLogin, getToken, setToken, clearToken, setCourseTypes, getCourseTypes, clearCourseTypes }
